package com.ygqh.baby.service.impl;

import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminContractDetail;
import com.ygqh.baby.mapper.AdminContractDetailMapper;
import com.ygqh.baby.model.PreSaleRemind;
import com.ygqh.baby.service.AdminContractDetailService;

import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.AdminContractDetailExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class AdminContractDetailServiceImpl implements AdminContractDetailService {

	@Autowired
	private AdminContractDetailMapper adminContractDetailMapper;

	@Override
	public List<AdminContractDetail> find() {
		AdminContractDetailExample example = new AdminContractDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminContractDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminContractDetailExample example = new AdminContractDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminContractDetailMapper.countByExample(example);
	}

	@Override
	public AdminContractDetail findById(Long id) {
		return adminContractDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(AdminContractDetail adminContractDetail) {
		return adminContractDetailMapper.insertSelective(adminContractDetail);
	}

	@Override
	public int update(AdminContractDetail adminContractDetail) {
		return adminContractDetailMapper.updateByPrimaryKeySelective(adminContractDetail);
	}

	@Override
	public ResultSet<AdminContractDetail> search(QueryInfo queryInfo, String q) {
		List<AdminContractDetail> list = adminContractDetailMapper.selectSuper(queryInfo, q);
		int count = adminContractDetailMapper.countSuper(q);
		ResultSet<AdminContractDetail> resultSet = new ResultSet<AdminContractDetail>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminContractDetailExample example = new AdminContractDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminContractDetail record = new AdminContractDetail();
		record.setStatus(DataStatus.Delete);
		return adminContractDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(AdminContractDetail adminContractDetail) {
		int r = 0;
		if (adminContractDetail.getId() != null) {
			r = this.update(adminContractDetail);
		} else {
			r = this.save(adminContractDetail);
		}
		return r;
	}

	@Override
	public AdminContractDetail findBySku(String skuCode, Long contractId) {
		AdminContractDetailExample example = new AdminContractDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andSkuCodeEqualTo(skuCode)
				.andContractIdEqualTo(contractId);
		List<AdminContractDetail> list = adminContractDetailMapper.selectByExample(example);
		if (list != null && list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public List<AdminContractDetail> findSkuListByContractId(Long contractId) {

		List<AdminContractDetail> list = adminContractDetailMapper.selectSkuListByContractId(contractId);

		return list;

	}

	@Override
	public List<PreSaleRemind> findPreSaleForRemind() {
		return adminContractDetailMapper.selectPreSaleForRemind();
	}

}
